<template>
	<a-card
		:bordered="false"
		:active-tab-key="cardActiveKey"
		:tab-list="tabListTitle"
		@tabChange="(key) => onTabChange(key)"
	>
		<div v-if="cardActiveKey === 'page'">
			<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
				<a-row :gutter="24">
					<a-col :span="6">
						<a-form-item label="关键词" name="searchKey">
							<a-input v-model:value="searchFormState.searchKey" placeholder="请输入关键词" />
						</a-form-item>
					</a-col>
					<a-col :span="6">
						<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
						<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
			<s-table
				ref="tableRef"
				:columns="columns"
				:data="loadData"
				:alert="options.alert.show"
				bordered
				:row-key="(record) => record.id"
				:tool-config="toolConfig"
				:row-selection="options.rowSelection"
				:scroll="{ x: 'max-content' }"
			>
				<template #operator class="table-operator">
					<a-space>
						<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('flwAgentAdd')">
							<template #icon><plus-outlined /></template>
							新增
						</a-button>
						<xn-batch-delete
							v-if="hasPerm('flwAgentBatchDelete')"
							:selectedRowKeys="selectedRowKeys"
							@batchDelete="deleteBatchFlwAgent"
						/>
					</a-space>
				</template>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'agentUserName'">
						<a-tag :bordered="false">
							{{ record.agentUserName }}
						</a-tag>
					</template>
					<template v-if="column.dataIndex === 'process'">
						<a-tag :bordered="false" color="processing" :key="process.id" v-for="process in JSON.parse(record.process)">
							{{ process.name }}
						</a-tag>
					</template>
					<template v-if="column.dataIndex === 'status'">
						<a-tag v-if="record.status === 'RUNNING'" color="processing" :bordered="false">
							{{ $TOOL.dictTypeData('FLW_AGENT_STATUS', record.status) }}
						</a-tag>
						<a-tag v-else color="warning" :bordered="false">{{
							$TOOL.dictTypeData('FLW_AGENT_STATUS', record.status)
						}}</a-tag>
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space v-if="record.status === 'RUNNING'">
							<a @click="formRef.onOpen(record)" v-if="hasPerm('flwAgentEdit')">编辑</a>
							<a-divider type="vertical" v-if="hasPerm(['flwAgentEdit', 'flwAgentEnd'], 'and')" />
							<a-popconfirm title="确定要结束吗？" @confirm="endFlwAgent(record)">
								<a style="color: rgb(255 165 0)" v-if="hasPerm('flwAgentEnd')">结束</a>
							</a-popconfirm>
							<a-divider type="vertical" v-if="hasPerm(['flwAgentEnd', 'flwAgentDelete'], 'and')" />
							<a-popconfirm title="确定要删除吗？" @confirm="deleteFlwAgent(record)">
								<a-button type="link" danger size="small" v-if="hasPerm('flwAgentDelete')">删除</a-button>
							</a-popconfirm>
						</a-space>
						<span v-else>
							无法编辑
							<a-divider type="vertical" v-if="hasPerm(['flwAgentDelete'], 'and')" />
							<a-popconfirm title="确定要删除吗？" @confirm="deleteFlwAgent(record)">
								<a-button type="link" danger size="small" v-if="hasPerm('flwAgentDelete')">删除</a-button>
							</a-popconfirm>
						</span>
					</template>
				</template>
			</s-table>
			<Form ref="formRef" @successful="tableRef.refresh()" />
		</div>
		<div v-if="cardActiveKey === 'agentPage'">
			<a-form
				ref="searchAgentFormRef"
				name="advanced_search"
				:model="searchAgentFormState"
				class="ant-advanced-search-form"
			>
				<a-row :gutter="24">
					<a-col :span="6">
						<a-form-item label="关键词" name="searchKey">
							<a-input v-model:value="searchAgentFormState.searchKey" placeholder="请输入关键词" />
						</a-form-item>
					</a-col>
					<a-col :span="6">
						<a-button type="primary" @click="tableAgentRef.refresh()">查询</a-button>
						<a-button style="margin: 0 8px" @click="resetAgent">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
			<s-table
				ref="tableAgentRef"
				:columns="columns"
				:data="loadData"
				:alert="options.alert.show"
				bordered
				:row-key="(record) => record.id"
				:tool-config="toolConfig"
				:row-selection="options.rowSelection"
				:scroll="{ x: 'max-content' }"
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'agentUserName'">
						<a-tag :bordered="false">
							{{ record.agentUserName }}
						</a-tag>
					</template>
					<template v-if="column.dataIndex === 'process'">
						<a-tag :bordered="false" color="processing" :key="process.id" v-for="process in JSON.parse(record.process)">
							{{ process.name }}
						</a-tag>
					</template>
					<template v-if="column.dataIndex === 'status'">
						<a-tag v-if="record.status === 'RUNNING'" color="processing" :bordered="false">
							{{ $TOOL.dictTypeData('FLW_AGENT_STATUS', record.status) }}
						</a-tag>
						<a-tag v-else color="warning" :bordered="false">{{
							$TOOL.dictTypeData('FLW_AGENT_STATUS', record.status)
						}}</a-tag>
					</template>
				</template>
			</s-table>
		</div>
	</a-card>
</template>

<script setup name="agentIndex">
	import { cloneDeep, remove } from 'lodash-es'
	import Form from './form.vue'
	import flwAgentApi from '@/api/flw/flwAgentApi'
	const cardActiveKey = ref('page')
	const tabListTitle = ref([
		{ key: 'page', tab: '发起委托' },
		{ key: 'agentPage', tab: '我的委托' }
	])
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const searchAgentFormState = ref({})
	const searchAgentFormRef = ref()
	const tableAgentRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = ref([
		{
			title: '受托人',
			dataIndex: 'agentUserName'
		},
		{
			title: '委托流程',
			dataIndex: 'process'
		},
		{
			title: '开始时间',
			dataIndex: 'startTime',
			width: '150px'
		},
		{
			title: '结束时间',
			dataIndex: 'endTime',
			width: '150px'
		},
		{
			title: '原因',
			dataIndex: 'cause'
		},
		{
			title: '委托状态',
			dataIndex: 'status',
			width: '100px'
		}
	])
	const columnsAction = {
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '200px'
	}
	// 操作栏通过权限判断是否显示
	if (cardActiveKey.value === 'page') {
		if (hasPerm(['flwAgentEdit', 'flwAgentDelete', 'flwAgentEnd'])) {
			columns.value.push(columnsAction)
		}
	} else {
		columns.value = remove((col) => col.dataIndex === 'action')
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const onTabChange = (value) => {
		cardActiveKey.value = value
		// 清空搜索表单
		searchFormState.value = {}
		searchAgentFormState.value = {}
		if (cardActiveKey.value === 'page') {
			if (hasPerm(['flwAgentEdit', 'flwAgentDelete', 'flwAgentEnd'])) {
				if (!columns.value.find((f) => f.dataIndex === 'action')) {
					columns.value.push(columnsAction)
				}
			}
		} else {
			if (columns.value.find((f) => f.dataIndex === 'action')) {
				remove(columns.value, (col) => col.dataIndex === 'action')
			}
		}
	}
	const loadData = (parameter) => {
		if (cardActiveKey.value === 'page') {
			const searchFormParam = cloneDeep(searchFormState.value)
			return flwAgentApi.flwAgentPage(Object.assign(parameter, searchFormParam)).then((data) => {
				return data
			})
		}
		if (cardActiveKey.value === 'agentPage') {
			const searchFormParam = cloneDeep(searchAgentFormState.value)
			return flwAgentApi.flwAgentMyPage(Object.assign(parameter, searchFormParam)).then((data) => {
				return data
			})
		}
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh()
	}
	// 重置
	const resetAgent = () => {
		searchAgentFormRef.value.resetFields()
		tableAgentRef.value.refresh()
	}
	// 结束
	const endFlwAgent = (record) => {
		let params = {
			id: record.id
		}
		flwAgentApi.flwAgentEnd(params).then(() => {
			tableRef.value.refresh()
		})
	}
	// 删除
	const deleteFlwAgent = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		flwAgentApi.flwAgentDelete(params).then(() => {
			tableRef.value.refresh()
		})
	}
	// 批量删除
	const deleteBatchFlwAgent = (params) => {
		flwAgentApi.flwAgentDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
